package com.software.levelupserver.utils;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class DateUtil {

    /**
     * 获取当前时间
     * @param pattern
     * @return
     */
    public static String getNowDate(String pattern){
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String date = currentDate.format(formatter);

        return date;
    }

    /**
     * 获取时间差，以分钟返回
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getMinutesDuration(String startTime, String endTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");

        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        Duration duration = Duration.between(start, end);

        //将Duration转化为分钟
        long minutes = duration.toMinutes();

        return minutes;
    }

    public static List<String> getFormattedThisWeekDates() {
        List<String> weekDates = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");

        for (int i = 0; i < 7; i++) {
            LocalDateTime dateTime = monday.plusDays(i);
            String formattedDateTime = dateTime.format(formatter);
            weekDates.add(formattedDateTime);
        }

        return weekDates;
    }

}
